//
//  DetailedViewController.swift
//  daily_count
//
//  Created by 段帅 on 2018/4/2.
//  Copyright © 2018年 Sooc. All rights reserved.
//

import UIKit
import MJRefresh
class DetailedViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
   
    

    @IBOutlet weak var table: UITableView!
    var month:String = String();
    var data = [[String:Any]]();
    var page = 1;
    let types = ["衣","食","住","行","其他"];
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.table.tableFooterView = UIView.init();
        
        print(month);
        createData();
        addMJRefresh();
        

        // Do any additional setup after loading the view.
    }

    
    func createData()  {
        
        
        if page == 1 {
            self.data.removeAll();
        }
        
        DSToolManager.postWithToken(URLString: severseUrl+api_getMonthCount, parameters: ["time":month as AnyObject,"page":page as AnyObject]) { (data_re, isSucess) in
            if (isSucess) {
                self.data = self.data + (data_re!["data"] as! [[String:Any]]);
                
                
                
                self.table.mj_header.endRefreshing();
                self.table.mj_footer.endRefreshing();
                if (self.data.count == (data_re!["total"] as! Int)){
                    self.table.mj_footer.endRefreshingWithNoMoreData();
                }
                
                self.table.reloadData(); 
            }else{
                DSToolManager.alertFaild(message: data_re!["statusCode"] as! String);
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "id", for: indexPath);
        let type = cell.contentView.viewWithTag(10) as! UILabel;
        type.text = String.init(format: "类型：%@", types[Int(data[indexPath.row]["type"] as! String)!]);
        let cost_label = cell.contentView.viewWithTag(11) as! UILabel;
        cost_label.text = String.init(format: "花费:%@", data[indexPath.row]["cost"] as! String);
        let content_label = cell.contentView.viewWithTag(12) as! UILabel;
        content_label.text = String.init(format: "花费说明:%@",data[indexPath.row]["comment"] as! String);
        let time_label = cell.contentView.viewWithTag(15) as! UILabel;
        time_label.text = String.init(format: "时间:%@",data[indexPath.row]["create_time"] as! String);
        
        
        
        
        
        return cell;
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140;
    }
    
    func addMJRefresh(){
        self.table.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            print("下拉刷新.")
            self.page = 1;
            self.createData();
            
        });
        self.table.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: { 
            self.page+=1;
            self.createData();
        });
        
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
